package com.prs.crm.service.ca.work.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.courser.ui.PaginationSupport;

import com.prs.crm.context.Attach;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.FileUtil;
import com.prs.crm.dao.ca.work.PurchaseApplyDao;
import com.prs.crm.domain.ca.work.ArticleTypes;
import com.prs.crm.domain.ca.work.DemandInventory;
import com.prs.crm.domain.ca.work.PurchaseApply;
import com.prs.crm.domain.cdxh.NewPlanOrder;
import com.prs.crm.domain.flow.FlowVerifyDetail;
import com.prs.crm.domain.flow.WorkFlowNodeVo;
import com.prs.crm.domain.flow.WorkFlowProcessNode;
import com.prs.crm.domain.flow.WorkFlowProcessNodeDetail;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.Apply;
import com.prs.crm.flow.FlowActionListener;
import com.prs.crm.flow.WorkFlowExecutor;
import com.prs.crm.service.ca.work.PurchaseApplyService;
import com.prs.crm.service.flow.WorkFlowProcessService;
import com.prs.crm.service.impl.BaseServiceImpl;

public class PurchaseApplyServiceImpl extends BaseServiceImpl<PurchaseApply, PurchaseApplyDao> implements PurchaseApplyService{

	private WorkFlowProcessService workflowProcessService;
	private WorkFlowExecutor workflowExecutor;
	public WorkFlowExecutor getWorkflowExecutor() {
		return workflowExecutor;
	}

	public void setWorkflowExecutor(WorkFlowExecutor workflowExecutor) {
		this.workflowExecutor = workflowExecutor;
	}

	public WorkFlowProcessService getWorkflowProcessService() {
		return workflowProcessService;
	}

	public void setWorkflowProcessService(
			WorkFlowProcessService workflowProcessService) {
		this.workflowProcessService = workflowProcessService;
	}

	@Override
	public List<PurchaseApply> getAllPurchaseApply() {
		return this.getDao().getAllPurchaseApply();
	}

	@Override
	public PurchaseApply getPurchaseApply(String paBillNumber) {
		return this.getDao().getPurchaseApply(paBillNumber);
	}

	@Override
	public void saveOrPurchaseApply(PurchaseApply purchaseApply) {
		this.getDao().saveOrPurchaseApply(purchaseApply);
	}

	@SuppressWarnings("unchecked")
	@Override
	public PaginationSupport listFlowTask(Class cls, PaginationSupport page,Person person) {
		page=this.getWorkflowProcessService().pageQueryFlowNode(cls, page, person, new Integer[] { Constrains.WORKFLOWPROCESS_STATE_PROCESS });
		if (page.getItems()!= null) {
			List<WorkFlowNodeVo> volist = new ArrayList<WorkFlowNodeVo>();
			for (WorkFlowProcessNode node : (Collection<WorkFlowProcessNode>)page.getItems()) {
				WorkFlowNodeVo vo=new WorkFlowNodeVo();
				vo.setEntity(this.getWorkflowProcessService().findProcessEntity(node.getFlowProcess(), cls));
				vo.setNode(node);
				volist.add(vo);
			}
			page.setItems(volist);
		}
		return page;
	}

	@Override
	public void execute(WorkFlowProcessNode node,
			FlowVerifyDetail detail, PurchaseApply entity,
			Person executor) {
		detail.setExecutor(executor);
//		Collection<WorkFlowProcessNode> nextNodes=this.getWorkflowExecutor().execute(node, detail, (FlowActionListener<?>)this);
//		if (nextNodes!=null) {
//			this.onNextStep(entity, nextNodes);
//		}
	}

	public void onNextStep(PurchaseApply entity, Collection<WorkFlowProcessNode> nextNodes) {

	}
//	@Override
//	public void savePurchaseApply(PurchaseApply purchaseApply,
//			List<ArticleTypes> di_articletype, Integer[] productlist,
//			String[] di_articleName, String[] di_articleStandard,
//			String[] di_articleNumber, String[] di_articleUnit,
//			String[] di_articleUnivalence, String[] prototalPrices) {
//		double totalPrice = 0.0;
//		for(int i=0;i<productlist.length;i++){
//			DemandInventory demoOb = new DemandInventory();	
//			demoOb.setDi_articletype(di_articletype.get(i));
////			System.out.println(demoOb);
//			demoOb.setDi_articleName(di_articleName[i]);
//			demoOb.setDi_articleNumber(di_articleNumber[i]);
//			demoOb.setDi_articleUnit(di_articleUnit[i]);
//			demoOb.setDi_articleUnivalence(di_articleUnivalence[i]);
//			demoOb.setDi_articleStandard(di_articleStandard[i]);
//			demoOb.setDi_money(prototalPrices[i]);
//			totalPrice = totalPrice + Double.parseDouble(prototalPrices[i]);
//			//this.getDemandInventoryService().submit(demoOb);
//			purchaseApply.addDemandInventory(demoOb);
//		}	
//		purchaseApply.setPa_totalMoney(totalPrice);
//		super.save(purchaseApply);
//	}

	@Override
	public void saveApply(PurchaseApply purchaseApply, File file,
			String fileName) {
		Attach attach=FileUtil.getInstance().saveFile(file, fileName,"",Constrains.UPLOAD_DIC_APPLY );
		if (attach.getSrc()!=null) {
			purchaseApply.getAttach().setPath(attach.getSrc());
		}
		this.getDao().save(purchaseApply);
	}
}
